﻿using System;
using System.Configuration;
using System.Windows;
using BizControl.Library.Security;
using BizControl.Presentation.Infrastructure.Properties;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using BizControl.Library.BusinessObjects.Models;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;

namespace BizControl.Presentation.Shell
{
  public partial class App : Application
  {
    protected override void OnStartup(StartupEventArgs e)
    {
      base.OnStartup(e);
      this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
#if (DEBUG)
      //RunInReleaseMode();
      RunInDebugMode();
#else
      RunInReleaseMode();
#endif
    }

    public static void RunInDebugMode()
    {
      string userName = @"michael";
      string password = @"passwordHash";
      BizControlPrincipal.Login(userName, password);
      // caching
      //ICacheManager cache = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>();
      //cache.Add("ItemOne", "ItemOneValue");
      //var cachedVal = cache.GetData("ItemOne");
      //
      // logging
      LogWriter defaultWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
      if (defaultWriter.IsLoggingEnabled())
        defaultWriter.Write(string.Format("User {0} succefully logged in with password {1}", userName, password),
          "General", 5, 9000, System.Diagnostics.TraceEventType.Information, "Log in");
      //
      ((BizControlPrincipal)Csla.ApplicationContext.User).Model = ModelBO.GetModel(Guid.Parse("af59cdda-a636-48f0-b5ae-1130030ee141"));
      Bootstraper bootstraper = new Bootstraper();
      bootstraper.Run(true);
    }

    public static void RunInReleaseMode()
    {
      IsLoggingOut = false;
      AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
      try
      {
        Login login = new Login();
        login.ShowDialog();
        if (Csla.ApplicationContext.User.Identity.IsAuthenticated)
        {
          ModelSelector modelSelector = new ModelSelector();
          modelSelector.ShowDialog();
          if (modelSelector.Result)
          {
            Bootstraper bootstraper = new Bootstraper();
            bootstraper.Run(true);
          }
          else
            Application.Current.Shutdown();
        }
        else
          Application.Current.Shutdown();
      }
      catch (Exception ex)
      {
        HandleException(ex);
      }
    }

    private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
      HandleException(e.ExceptionObject as Exception);
    }

    private static void HandleException(Exception ex)
    {
      if (null == ex)
        return;

      //ExceptionPolicy.HandleException(ex, "Default Policy");
      MessageBox.Show(string.Format("{0} {1} {2} {3}",
        BizControl.Presentation.Infrastructure.Properties.ErrorMessages.UnhandledException, Environment.NewLine, ex.Message, ex.Source));
      Environment.Exit(1);
    }

    public static bool IsLoggingOut
    {
      get;
      set;
    }
  }
}
